package com.kxf.androidtestdemo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class StackLayout extends ViewGroup {
    public StackLayout(Context context) {
        super(context);
    }

    public StackLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StackLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 2、根据自定义View的绘制流程，重写`onMeasure`方法，注意对wrap_content的处理
     * 重写onMeasure方法
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取宽高的测量模式以及测量值
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //测量所有子View
        measureChildren(widthMeasureSpec, heightMeasureSpec);
//        //如果没有子View，则View大小为0，0
//        if (getChildCount() == 0) {
//            setMeasuredDimension(0, 0);
//        } else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
//            View childOne = getChildAt(0);
//            int childWidth = childOne.getMeasuredWidth();
//            int childHeight = childOne.getMeasuredHeight();
//            //View的宽度=单个子View宽度*子View个数，View的高度=子View高度
//            setMeasuredDimension(getChildCount() * childWidth, childHeight);
//        } else if (widthMode == MeasureSpec.AT_MOST) {
//            View childOne = getChildAt(0);
//            int childWidth = childOne.getMeasuredWidth();
//            //View的宽度=单个子View宽度*子View个数，View的高度=xml当中设置的高度
//            setMeasuredDimension(getChildCount() * childWidth, heightSize);
//        } else if (heightMode == MeasureSpec.AT_MOST) {
//            View childOne = getChildAt(0);
//            int childHeight = childOne.getMeasuredHeight();
//            //View的宽度=xml当中设置的宽度，View的高度=子View高度
//            setMeasuredDimension(widthSize, childHeight);
//        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int top = getMeasuredHeight();
        Log.d("StackLayout", "getMeasuredHeight() = " + getMeasuredHeight());
        Log.d("StackLayout", "getMeasuredWidth() = " + getMeasuredWidth());
        View child;
        for (int i = 0; i < childCount; i++) {
            child = getChildAt(i);
            int left = (getMeasuredWidth()-child.getMeasuredWidth())/2;
            if (child.getVisibility() != View.GONE) {
                int childHight = child.getMeasuredHeight();
                child.layout(left, top - child.getMeasuredHeight(), left + child.getMeasuredWidth(), top);
                top -= childHight;
            }
        }
    }


}
